<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { accountApi } from '@/api/controller';
  import { rechargeChannels } from '@/pinia/getter';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  interface formQuert {
    user_id:number
    content:string
    pay_type:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, trigger: 'blur', message: '请选择封禁用户' }],
    content: [{ required: true, trigger: 'blur', message: '请输入封禁说明' }],
    pay_type: [{ required: true, trigger: 'blur', message: '请选择充值渠道' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await accountApi.setRechargeBlackAdd(form).then(res=>{
        loading.close()
        ToastSuccess('新增成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = ()=> {
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="8vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    title="充值黑名单">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="充值渠道" prop="pay_type">
          <el-select v-model="form.pay_type" placeholder="请选择充值渠道">
            <el-option
              v-for="item in rechargeChannels"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="封禁说明" prop="content">
          <el-input
            :disabled="action.disabled"
            v-model="form.content"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入封禁说明"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>